package com.kupurui.asteacher.ui.job;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.frame.util.AppJsonUtil;
import com.android.frame.util.Toolkit;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.adapter.ScreenRuleAdapter;
import com.kupurui.asteacher.adapter.SelKnowledgeAdapter;
import com.kupurui.asteacher.bean.SelKnowledgeInfo;
import com.kupurui.asteacher.http.Teacher;
import com.kupurui.asteacher.ui.BaseAty;
import com.kupurui.asteacher.utils.UserManger;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import info.hoang8f.widget.FButton;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 知识点选择
 * Created by Administrator on 2017/7/24.
 */

public class SelKnowledgeAty extends BaseAty {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_knowledge_name)
    TextView tvKnowledgeName;
    @Bind(R.id.tv_num)
    TextView tvNum;
    @Bind(R.id.listview)
    ListView listview;
    @Bind(R.id.ll_sel)
    LinearLayout llSel;
    @Bind(R.id.fbtn_preview)
    FButton fbtnPreview;

    PopupWindow popupWindow;

    SelKnowledgeInfo selKnowledgeInfo;
    List<SelKnowledgeInfo.DownBean> list;
    SelKnowledgeAdapter adapter;

    private String chapter_id = "";
    private String name = "";

    @Override
    public int getLayoutId() {
        return R.layout.sel_knowledge_aty;
    }

    @Override
    public void initData() {

        if (getIntent().getExtras()!= null){
            chapter_id = getIntent().getStringExtra("id");
            name = getIntent().getStringExtra("name");
        }
        tvTitle.setText(name);
        list = new ArrayList<>();
        adapter = new SelKnowledgeAdapter(this,list,R.layout.sel_knowledge_item);

        listview.setAdapter(adapter);
        fbtnPreview.setText("已选"+UserManger.subject_id.size()+"道习题,点击预览");
    }

    @Override
    public void requestData() {
        showLoadingContent();
        new Teacher().chooseChapter(chapter_id,this,0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        fbtnPreview.setText("已选"+UserManger.subject_id.size()+"道习题,点击预览");
    }

    @OnClick({R.id.iv_back,R.id.tv_title,R.id.fbtn_preview,R.id.ll_sel})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_title:
//                screen();
                break;
            case R.id.fbtn_preview://预览
                if (Toolkit.listIsEmpty(UserManger.subject_id)){
                    showToast("请先选择题目");
                    return;
                }
                startActivity(PreviewAty.class,null);
                break;
            case R.id.ll_sel://第一级
                Bundle bundle = new Bundle();
                bundle.putString("type","1");
                bundle.putString("id",chapter_id);
                startActivity(JobSelAty.class,bundle);
                break;
        }
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        switch (what){
            case 0:
                selKnowledgeInfo = AppJsonUtil.getObject(result,SelKnowledgeInfo.class);
                tvTitle.setText(selKnowledgeInfo.getUp().getTitle());
                tvKnowledgeName.setText(selKnowledgeInfo.getUp().getTitle());
                tvNum.setText(selKnowledgeInfo.getUp().getTotal());
                list.clear();
                list.addAll(selKnowledgeInfo.getDown());
                adapter.notifyDataSetChanged();
                break;
        }
        super.onSuccess(result, call, response, what);
    }

    private void screen() {
        if (popupWindow == null) {
            popupWindow = new PopupWindow(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
            View view = LayoutInflater.from(this).inflate(R.layout.window_screen_rule_layout, null);
            popupWindow.setContentView(view);
            View tvBack =  view.findViewById(R.id.tv_back);
            tvBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow.dismiss();
                }
            });
            ListView mlistView = (ListView) view.findViewById(R.id.listview);
            final List<String> list = new ArrayList<>();
            list.add("数与式");
            list.add("方程与不等式");
            list.add("函数");
            list.add("图形与几何");
            list.add("图形的变化");
            final ScreenRuleAdapter adapter = new ScreenRuleAdapter(this, list, R.layout.textview_layout);
            mlistView.setAdapter(adapter);
            popupWindow.setBackgroundDrawable(new ColorDrawable());
            popupWindow.setOutsideTouchable(true);

            mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    adapter.setChooseIndex(i);
                    adapter.notifyDataSetChanged();
                    popupWindow.dismiss();
                    tvTitle.setText(list.get(i));
                }
            });

            popupWindow.showAsDropDown(tvTitle);
            popupWindow.showAsDropDown(tvTitle);

        } else {
            popupWindow.showAsDropDown(tvTitle);

        }

    }

}
